package com.ccrfid.rmc6.resource.mattress;

import com.ccrfid.rmc6.appconfig.exception.pojo.AppException;
import com.ccrfid.rmc6.entity.mattress.BedEntity;
import com.ccrfid.rmc6.entity.mattress.BedHistoryEntity;
import com.ccrfid.rmc6.entity.mattress.MattressStateEntity;
import com.ccrfid.rmc6.pojo.dto.mattress.BedLayoutDto;
import com.ccrfid.rmc6.pojo.param.PageParam;
import com.ccrfid.rmc6.repository.search.Criterion;
import com.ccrfid.rmc6.repository.search.SearchCriteria;
import com.ccrfid.rmc6.service.mattress.BedService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.util.StringUtils;

import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import java.sql.Timestamp;
import java.util.List;
import java.util.Set;

@Path("beds")
@Produces(MediaType.APPLICATION_JSON)
public class BedResource {

    @Autowired
    private BedService bedService;

    //获取床位布局
    @GET
    @Path("row-col-layout")
    public BedLayoutDto getBedLayout() {
        return bedService.getBedLayout();
    }

    //获取床位信息
    @GET
    @Path("all")
    public List<BedEntity> getAllBeds() {
        return bedService.getAllBeds();
    }

    //新增床位信息
    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    public BedEntity createBed(BedEntity bedEntity) throws AppException {
        return bedService.createBed(bedEntity);
    }

    //修改床位绑定的床垫信息
    @PUT
    @Path("{bedSn}/mattress/{mattressSn}")
    public BedEntity updateBedMattress(@PathParam("bedSn") String bedSn, @PathParam("mattressSn") String mattressSn)
            throws AppException {
        return bedService.updateBedMattress(bedSn, mattressSn);
    }

    //删除床位信息
    @DELETE
    @Path("{bedSn}")
    public Boolean deleteBed(@PathParam("bedSn") String bedSn) throws AppException {
        return bedService.deleteBed(bedSn);
    }

    //获取可用床垫信息
    @GET
    @Path("available-mattresses")
    public Set<String> getAvailableMattresses() {
        return bedService.getAvailableMattresses();
    }

    //人员入住床位
    @PUT
    @Path("{bedSn}/check-in")
    @Consumes(MediaType.APPLICATION_JSON)
    public BedEntity bedCheckIn(@PathParam("bedSn") String bedSn, BedEntity bedEntity) throws AppException {
        return bedService.bedCheckIn(bedSn, bedEntity);
    }

    //人员退出床位
    @PUT
    @Path("{bedSn}/check-out")
    public Boolean bedCheckOut(@PathParam("bedSn") String bedSn) throws AppException {
        return bedService.bedCheckOut(bedSn);
    }

    //查询某个床位人员的入住时间
    @GET
    @Path("{bedSn}/check-in-time")
    public BedHistoryEntity getBedCheckInTime(@PathParam("bedSn") String bedSn) {

        return bedService.getBedCheckInTime(bedSn);
    }

    //获取床垫历史数据
    @GET
    @Path("mattress/{mattressSn}/state-history")
    public List<MattressStateEntity> getMattressStateHistory(@PathParam("mattressSn") String mattressSn,
                                                             @QueryParam("startTime") Timestamp startTime,
                                                             @QueryParam("stopTime") Timestamp stopTime) {
        return bedService.getMattressStateHistory(mattressSn, startTime, stopTime);
    }

    //分页获取床垫历史数据
    @GET
    @Path("mattress/{mattressSn}/state-history-page")
    public Page<MattressStateEntity> getMattressStateHistoryPage(@PathParam("mattressSn") String mattressSn,
                                                                 @QueryParam("startTime") Timestamp startTime,
                                                                 @QueryParam("stopTime") Timestamp stopTime,
                                                                 @BeanParam PageParam pageParam) {
        SearchCriteria criteria = SearchCriteria.Builder.create();
        if (!StringUtils.isEmpty(mattressSn)) {
            criteria.add(new Criterion("mattressSn", Criterion.Operator.EQ, mattressSn));
        }
        if (startTime != null) {
            criteria.add(new Criterion("recordTime", Criterion.Operator.GTE, startTime));
        }
        if (stopTime != null) {
            criteria.add(new Criterion("recordTime", Criterion.Operator.LTE, stopTime));
        }
        PageRequest pageRequest;
        if (pageParam.getOrders() != null && pageParam.getOrders().size() > 0) {
            pageRequest = PageRequest.of(pageParam.getPageNumber(), pageParam.getPageSize(), Sort.by(pageParam.getOrders()));
        } else {
            pageRequest = PageRequest.of(pageParam.getPageNumber(), pageParam.getPageSize());
        }
        return bedService.getMattressStateHistoryPage(criteria, pageRequest);
    }
}
